package com.guofen.service.hsmpy.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.HsmMiddleWormMapper;
import com.guofen.pojo.HsmMiddleWorm;
import com.guofen.service.hsmpy.MiddleWormBreedingMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MiddleWormBreedingMaterialServiceImpl implements MiddleWormBreedingMaterialService {

    @Autowired
    private HsmMiddleWormMapper hsmMiddleWormMapper;

    @Override
    public boolean add(Integer id, String big_bug_breed_end_time, String go_where) {
        HsmMiddleWorm hsmMiddleWorm = hsmMiddleWormMapper.selectByPrimaryKey(id);
        hsmMiddleWorm.setBigBugBreedEndTime(big_bug_breed_end_time);
//        hsmMiddleWorm.setAfterBigWeight(after_big_w);
        hsmMiddleWorm.setGoWhere(go_where);
        int result = hsmMiddleWormMapper.updateByPrimaryKey(hsmMiddleWorm);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<Map<String, HsmMiddleWorm>> getlatest() {
        return hsmMiddleWormMapper.getMiddleWormBreedingMaterialLatest();
    }

    @Override
    public List<Map<String, HsmMiddleWorm>> getlist() {
        return hsmMiddleWormMapper.getMiddleWormBreedingMaterialList();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<HsmMiddleWorm> list = hsmMiddleWormMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<HsmMiddleWorm> list = hsmMiddleWormMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return hsmMiddleWormMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(HsmMiddleWorm record) {
        return hsmMiddleWormMapper.updateByPrimaryKeySelective(record);
    }
}
